package civicinity.ist.psu.edu.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import civicinity.ist.psu.edu.R;
import civicinity.ist.psu.edu.adapter.CivicinityRSSInfoAdapter.ViewHolder;
import civicinity.ist.psu.edu.constants.Constants;
import civicinity.ist.psu.edu.events.CivicinityCalFeed.RSSCalInfo;
import civicinity.ist.psu.edu.rssfeed.RSSDBItem;
import civicinity.ist.psu.edu.rssfeed.RSSItem;

public class CivicinityRSSCalAdapter extends BaseAdapter{
	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<RSSCalInfo> mArr;
	private int mLayout;
	private String mCurrentTime;
	
	public CivicinityRSSCalAdapter(Context context, int layout, ArrayList<RSSCalInfo> arr) {
		mContext = context;
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mLayout = layout;
		mArr = arr;
		
		long timeInMillis = System.currentTimeMillis();
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
        Date resultdate = new Date(timeInMillis);
        mCurrentTime = sdf.format(resultdate);
        
        Log.i("K", "millis : " + timeInMillis  + "cTime : " + mCurrentTime);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArr.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mArr.get(position).eTitle;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null)
			convertView = mInflater.inflate(mLayout, parent, false);
		
		TextView vTitle = (TextView)convertView.findViewById(R.id.rss_title);
		TextView vPubDate = (TextView)convertView.findViewById(R.id.rss_feed_date);
		ImageView vCheck = (ImageView)convertView.findViewById(R.id.check_mark);

		vTitle.setText(mArr.get(position).eTitle);
		
        // compare current time and the schedule time,
        // if the event has been already passed, show check mark.
        if(mCurrentTime.compareTo(mArr.get(position).eStartDate) > 0) {
        	vPubDate.setText("Done");
        	vCheck.setVisibility(View.VISIBLE);
        	vCheck.setBackgroundResource(R.drawable.check_mark);
        }
        else {
        	vPubDate.setText(mArr.get(position).eStartDate);// + " ~ " + mArr.get(position).eEndDate);
        	vCheck.setVisibility(View.GONE);
        	//vCheck.setBackgroundResource(R.drawable.icon);
        }
        
		//vPubDate.setText(sdf.format(resultdate));
		//vPubDate.setText(mArr.get(position).eDescription);
		
		// TODO Auto-generated method stub
		return convertView;
	}
}
